/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javamail.sip;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.ListIterator;
import javamail.sdp.sdpMessage;
import javamail.sip.util.eSipHeader;

/**
 *
 * @author sriram
 */
public class sipGenerator {
    private static String sip_user = javamail.main.glblConfig.getSipUser();
    private static String sip_interface = javamail.main.glblConfig.getSipInterface();
    private static int sip_port = javamail.main.glblConfig.getSipPort();
    private static final String defaultFrom = "<sip:"+setSipInterface(sip_interface)+":"+sip_port+">";
    private static final String defaultVia = setSipInterface(sip_interface)+":"+sip_port;

    private static String setSipInterface(String ifc) {
        if(ifc.equals("0.0.0.0") || ifc.startsWith("127.0")) {
            try {
                String to = InetAddress.getLocalHost().getHostAddress();
                String[] result = to.split(":");
                if(result[0].equals("sip"))
                    ifc = result[1];
                else
                    ifc = result[0];
            } catch (UnknownHostException e) {}
        }
        return ifc;
    }

    public static sipMessage addVia(sipMessage sip) {
        Calendar cal = Calendar.getInstance();
        sip.headers[eSipHeader.VIA.toInt()] = "SIP/2.0/UDP "+defaultVia+";rport"+";branch=z9hG4bK"+cal.getTimeInMillis()+cal.getTime().hashCode();
        return sip;
    }
    public static void addVia(sipMessage sip, String via) {
        sip.headers[eSipHeader.VIA.toInt()] = via;
    }
    public static void addMaxForwards(sipMessage sip) {
        sip.headers[eSipHeader.MAX_FORWARDS.toInt()] = "70";
    }
    public static void addFromWithTag(sipMessage sip, String tag) {
        sip.headers[eSipHeader.FROM.toInt()] = defaultFrom+tag;
    }
    public static void addFrom(sipMessage sip, String from) {
        sip.headers[eSipHeader.FROM.toInt()] = from;
    }
    public static void addTo(sipMessage sip, String to) {
        sip.headers[eSipHeader.TO.toInt()] = to;
    }
    public static void addTo(sipMessage sip, String to, String tag) {
        // Add the to tag only if there is not tag already.
        if(to.indexOf("tag=")<0)
            sip.headers[eSipHeader.TO.toInt()] = to+tag;
        else
            sip.headers[eSipHeader.TO.toInt()] = to;
    }
    public static void addContact(sipMessage sip, String contact) {
        if(contact.contains("0.0.0.0") || contact.contains("127.0.0.") || contact.contains("127.0.1.")) {
            sip.headers[eSipHeader.CONTACT.toInt()] = defaultVia;
        } else
            sip.headers[eSipHeader.CONTACT.toInt()] = contact;
    }
    public static void addServer(sipMessage sip) {
        sip.headers[eSipHeader.SERVER.toInt()] = "Sriram SIP Answering Mach 1.0";
    }
    public static void addCallId(sipMessage sip, String callid) {
        sip.headers[eSipHeader.CALLID.toInt()] = callid;
    }
    public static void addCSeq(sipMessage sip, String cseq) {
        sip.headers[eSipHeader.CSEQ.toInt()] = cseq;
    }
    public static void addContentLength(sipMessage sip, int length) {
        sip.headers[eSipHeader.CONTENT_LENGTH.toInt()] = " "+length;
    }
    public static void addContentType(sipMessage sip, String type) {
        sip.headers[eSipHeader.CONTENT_TYPE.toInt()] = type;
    }
    public static sipMessage addMessageBody(sipMessage sip, sdpMessage sdp) {
        String[] line = sdp.toString().split("\r\n");
        for(int i=0; i<line.length; i++)
            sip.body.add(line[i]);
        return sip;
    }
    public static String toString(sipMessage msg) {
        String str = new String("");
        if(msg.isRequest) {
            str += msg.method.name() + " sip:"+setSipInterface(sip_interface)+":"+sip_port+" SIP/2.0\r\n";
       }
        else {
            str += "SIP/2.0 " + msg.status.toCode() + " " +msg.status.toString() + "\r\n";
        }
        for(int i=0; i<msg.headers.length; i++) {
            if(msg.headers[i] != null && !msg.headers[i].equals("")) {
                eSipHeader temp = eSipHeader.fromInt(i);
                if(temp != eSipHeader.NIL)
                    str += temp.toString() + msg.headers[i] + "\r\n";
            }
        }
        str += "\r\n";
        if(msg.containsBody()) {
            ListIterator list = msg.body.listIterator();
            while(list.hasNext()) {
                String line = (String)list.next();
                if(line.trim().equals("\r\n"))
                    str += line;
                else
                    str += line + "\r\n";
            }
        }
        return str;
    }
}
